import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {AxdrSequenceOf} from "../asn1.axdr/AxdrSequenceOf";
import {READRETOMDDATA} from "./READRETOMDDATA";
import {Buffer} from "buffer";
import {PIID} from "./PIID";

export class SubSeqOfReads extends AxdrSequenceOf<READRETOMDDATA>{

    createListElement(): READRETOMDDATA {
        return new READRETOMDDATA();
    }

    constructor() {
        super();
    }

    set_length(length: number) {
        super.set_length(length);
    }

}

export class ActionThenGetResponseNormalList implements AxdrType{

    dataCode : Buffer | null = null;
    piid_acd : PIID | null = null;
    reads : SubSeqOfReads | null = null;

    constructor() {
    }

    set_dataCode(dataCode : Buffer){
        this.dataCode = dataCode;
    }

    set_all(piid_acd : PIID, reads : SubSeqOfReads){
        this.piid_acd = piid_acd;
        this.reads = reads;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;

        this.piid_acd = new PIID();
        this.piid_acd.setNumBit(8);
        codeLength += this.piid_acd.decode(input);

        this.reads = new SubSeqOfReads();
        codeLength += this.reads.decode(input);

        return codeLength;
    }

    encode(output: ReverseByteArrayOutputStream): number {
        let codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if(this.reads != null && this.piid_acd != null){
                codeLength = 0;
                codeLength += this.reads.encode(output);
                codeLength += this.piid_acd.encode(output);
            }else{
                throw new Error("piid_acd or reads is null");
            }
        }
        return codeLength;
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    toString(): string {
        if (this.piid_acd != null && this.reads != null) {
            return "sequence: {"+ "piid_acd: " + this.piid_acd + ", reads: " + this.reads  + "}";
        } else {
            return "piid_acd or reads is null";
        }
    }

}
